package edu.hawaii.myisern.action;

import static org.junit.Assert.assertEquals;

import org.junit.Test;

import com.meterware.httpunit.WebConversation;
import com.meterware.httpunit.WebForm;
import com.meterware.httpunit.WebRequest;
import com.meterware.httpunit.WebResponse;

/**
 * Provides test cases.
 * 
 * @author Jon Lao
 * @author Jared Sunouchi
 * @author Chiao-Fen Zielinski-Liu
 */
public class TestCollaborationActionBean {
  
  /** Get the test host. */
  private String testHost = System.getProperty("test.host");
  
  /** The page title. */
  private String homeTitle = "MyISERN 1.3";
  
  private String username = "username";
  private String password = "password";
  private String addTitle = homeTitle + " - Add";
  private String editTitle = homeTitle + " - Edit";
  private String collabForm = "CollaborationForm";
  private String name = "name";

  
  /**
   * Tests the collaboration function for adding under normal situations.
   *
   * @throws Exception If problems occur.
   */
  @Test
  @SuppressWarnings("deprecation") //TableCell.asText() is httpunit 1.5.3, needed due to Hackystat
  public void testAddCollaborationActionBean() throws Exception {    
    WebConversation conversation = new WebConversation();
    String url = testHost + "myisern-1-green/index.jsp";
    WebResponse response = conversation.getResponse(url);
    assertEquals("Checking index.jsp retrieval", "MyISERN 1.3 Login", response.getTitle());
    
    WebForm loginForm = response.getFormWithID("LoginForm");
    WebRequest loginRequest = loginForm.getRequest();
    response = conversation.getResponse(loginRequest);
    loginRequest = loginForm.getRequest();
    
    loginRequest.setParameter(username, username);
    loginRequest.setParameter(password, password);
    
    response = conversation.getResponse(loginRequest);
    assertEquals("Checking home.jsp retrieval", homeTitle, response.getTitle());
    
    url = testHost + "myisern-1-green/add.jsp";
    response = conversation.getResponse(url);
    assertEquals("Checking add.jsp retrieval", addTitle, response.getTitle());
    
    WebForm collaborationForm = response.getFormWithID("AddCollaborationForm");
    WebRequest collaborationRequest = collaborationForm.getRequest();
    response = conversation.getResponse(collaborationRequest);
    assertEquals("Checking Collaboration.action retrieval", addTitle, response.getTitle());
    
    WebForm addCollaborationForm = response.getFormWithID(collabForm);
    WebRequest addCollaborationRequest = addCollaborationForm.getRequest();
    addCollaborationRequest.setParameter(name, "");
    response = conversation.getResponse(addCollaborationRequest);
    
    addCollaborationForm = response.getFormWithID(collabForm);
    addCollaborationRequest = addCollaborationForm.getRequest();
    
    addCollaborationRequest.setParameter(name, "Test Name");
    addCollaborationRequest.setParameter("collaboratingOrganizations", 
        "Test Collaborating Organization 1, Test Collaborating Organization 2");
    addCollaborationRequest.setParameter("collaborationTypes", 
        "Test Collaboration Type 1, Test Collaboration Types 2");
    addCollaborationRequest.setParameter("years", "2006, 2007");
    addCollaborationRequest.setParameter("outcomeTypes", 
        "Test Outcome Type 1, Test Outcome Type 2");
    addCollaborationRequest.setParameter("description", "Test Description");
    
    response = conversation.getResponse(addCollaborationRequest);
    
    url = testHost + "myisern-1-green/edit.jsp";
    response = conversation.getResponse(url);
    assertEquals("Checking edit.jsp retrieval", editTitle, response.getTitle());

    collaborationForm = response.getFormWithID("EditCollaborationForm");
    collaborationRequest = collaborationForm.getRequest();
    response = conversation.getResponse(collaborationRequest);
    
    WebForm editCollaborationForm = response.getFormWithID(collabForm);
    WebRequest editCollaborationRequest = editCollaborationForm.getRequest();
    
    editCollaborationRequest.setParameter(name, "Test Name");
    
    response = conversation.getResponse(editCollaborationRequest);
    
    collaborationForm = response.getFormWithID(collabForm);
    collaborationRequest = collaborationForm.getRequest("edit");
    response = conversation.getResponse(collaborationRequest);
  }
  
  /**
   * Tests the collaboration function for editing under normal situations.
   *
   * @throws Exception If problems occur.
   */
  @Test
  public void testEditCollaborationActionBean() throws Exception {
    WebConversation conversation = new WebConversation();
    String url = testHost + "myisern-1-green/index.jsp";
    WebResponse response = conversation.getResponse(url);
    assertEquals("Checking index.jsp retrieval", "MyISERN 1.3 Login", response.getTitle());
    
    WebForm loginForm = response.getFormWithID("LoginForm");
    WebRequest loginRequest = loginForm.getRequest();
    response = conversation.getResponse(loginRequest);
    loginRequest = loginForm.getRequest();
    
    loginRequest.setParameter(username, username);
    loginRequest.setParameter(password, password);
    
    response = conversation.getResponse(loginRequest);
    assertEquals("Checking home.jsp retrieval", homeTitle, response.getTitle());
    
    url = testHost + "myisern-1-green/edit.jsp";
    response = conversation.getResponse(url);
    assertEquals("Checking edit.jsp retrieval", editTitle, response.getTitle());
    
    WebForm collaborationForm = response.getFormWithID("EditCollaborationForm");
    WebRequest collaborationRequest = collaborationForm.getRequest();
    response = conversation.getResponse(collaborationRequest);
    
    WebForm editCollaborationForm = response.getFormWithID(collabForm);
    WebRequest editCollaborationRequest = editCollaborationForm.getRequest();
    
    editCollaborationRequest.setParameter(name, "Test Name");
    
    response = conversation.getResponse(editCollaborationRequest);
    
    editCollaborationForm = response.getFormWithID(collabForm);
    editCollaborationRequest = editCollaborationForm.getRequest("edit");
    
    editCollaborationRequest.setParameter("description", "Test Description Edited");
    
    response = conversation.getResponse(editCollaborationRequest);
    
    editCollaborationForm = response.getFormWithID(collabForm);
    editCollaborationRequest = editCollaborationForm.getRequest("remove");
    response = conversation.getResponse(editCollaborationRequest);
  }
}
